<?php
namespace App\Controllers;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

use App\Models\UserModel;
use stdClass;
 date_default_timezone_set('prc');
class UserController extends BaseController {
	
	public function UserList($request, $response, $args) {
		include APP_PATH . '/Views/User/list.html';
	}

	public function Error($request, $response, $args) {
		include APP_PATH . '/Views/405.html';
	}
public function Error404($request, $response, $args) {
		include APP_PATH . '/Views/404.html';
	}
	public function postLogin($request, $response, $args) {
		$username = $request -> getParam('username');
		$password = $request -> getParam('password');
		$user = new UserModel();
		if ($user -> getUserInfo($username, $password)) {
			$_SESSION["UserName"] = $username;
			$data = array('message' => '成功', 'code' => 0,"username"=>$username);
		} else {
			$data = array('message' => '用户名或密码不正确', 'code' => 101);
		}
		//		return $response -> withJson($data);
		echo json_encode($data);
	}
	
	public function Menu($request, $response, $args) {
		$user = new UserModel();
		$menu="menu";
		$username=$_SESSION["UserName"];
		$select=$user -> menu($username,$menu);
		$img=$user -> Cha($username);
		echo json_encode([
            'username' => $username,
            'menu' =>$select,
            'img'=>$img
        ]);
		
	}
	
	public function Cha($request, $response, $args) {
		$user = new UserModel();
		$username=$_SESSION["UserName"];
		$select=$user -> Cha($username);
		echo json_encode([
            'userlist' =>$select
        ]);
		
	}
	
	public function Chaid($request, $response, $args) {
		$userid = $request -> getParam('userid');
		$user = new UserModel();
		$select=$user -> select($userid);
		echo json_encode([
            'userlist' =>$select
        ]);
		
	}
	
	public function Pas($request, $response, $args) {
		$user = new UserModel();
		$pas = $request -> getParam('pas');
		$data=array('Password' =>$pas);
		$username=$_SESSION["UserName"];
		$select=$user -> Pas($username,$data);
		echo json_encode([
            'userpas' =>$select
        ]);
		
	}
	public function HeadImg($request, $response, $args) {
		$user = new UserModel();
		$address = $request -> getParam('address');
		$sex = $request -> getParam('sex');
		$phone = $request -> getParam('phone');
		$email = $request -> getParam('email');
		$age = $request -> getParam('age');
		$time=date('Y-m-d H:i:s',time());
		$data = array('address' =>$address, 'age' => $age, 'Phone' => $phone,'Email' => $email,'Sex'=>$sex, 'Time'=>$time,);
		$username=$_SESSION["UserName"];
		$select=$user -> Pas($username,$data);
		
	}
	public function xgimg($request, $response, $args) {
		$srcName = $_FILES['upload_file']['name'];
		function get_basename($filename)  
		{  
		    return preg_replace('/^.+[\\\\\\/]/', '', $filename);  
		}  
		copy($_FILES['upload_file']['tmp_name'],"static/img/".iconv("UTF-8","gb2312",get_basename($srcName)));  
		
		$src = "/static/img/$srcName";
		$username=$_SESSION["UserName"];
		$data = array('Face' =>$srcName);
		$user = new UserModel();
		$ret = $user -> Pas($username,$data);	
		if($ret != 0){
			return $response -> withRedirect($this -> container -> router -> pathFor('UserInfo'));
		}
//		echo json_encode([
//          'msg' => $src
//      ]);
	}
	public function Login($request, $response, $args) {
		include APP_PATH . '/Views/User/Login.html';
	}
	
	public function Logout($request, $response, $args) {
		unset($_SESSION["UserName"]);
		include APP_PATH . '/Views/User/Login.html';
	}

	public function UserInfo($request, $response, $args) {
		include APP_PATH . '/Views/User/info.html';
	}
	public function UserPas($request, $response, $args) {
		include APP_PATH . '/Views/User/mima.html';
	}
	
	public function UserLook($request, $response, $args) {
		include APP_PATH . '/Views/User/cha.html';

	}
	public function Img($request, $response, $args) {
		include APP_PATH . '/Views/User/HeadImg.html';

	}
	
	public function UserNameSelect($request, $response, $args){
		$val = $request -> getParam('val');
		$user = new UserModel();
		$s=$user -> NameSelect($val);
		echo json_encode([
            'msg' => $s
        ]);
	}
	public function UserAdd($request, $response, $args) {
		$username = $request -> getParam('name');
		$age = $request -> getParam('age');
		$roleid = $request -> getParam('roleid');
		$phone = $request -> getParam('phone');
		$address = $request -> getParam('address');
		$sex = $request -> getParam('sex');
		$email = $request -> getParam('email');
		$pas = $request -> getParam('pas');
		$time=date('Y-m-d H:i:s',time());
		$user = new UserModel();
		$data = array('UserName' =>$username, 'age' => $age, 'Phone'=>$phone,'RoleID' => $roleid,'Sex'=>$sex,'address'=>$address,'Email' => $email, 'Password' => $pas,'Time'=>$time,'Face'=>"user4-128x128.jpg");
		$user -> add($data);
		echo json_encode([
            'code' => 0,
            'msg' => 'success'
        ]);
		
		
	}
	
	public function UserDelete($request, $response, $args) {
		$id = $request -> getParam('id');
		$user = new UserModel();
		$user -> delete($id);
		echo json_encode([
            'code' => 0,
            'msg' => 'success'
        ]);
	}

	public function RoleUserUpdate($request, $response, $args) {
//		echo "UserUpdate";
		$roleid = $request -> getParam('roleid');
		$id = $request -> getParam('id');
		$time=date('Y-m-d H:i:s',time());
		$user = new UserModel();
		$data = array('RoleID' => $roleid,'Time'=>$time,);
		$user -> update($id,$data);
		echo json_encode([
            'code' => 0,
            'msg' => 'success'
        ]);
	}
	public function UserUpdate($request, $response, $args) {
//		echo "UserUpdate";
		$username = $request -> getParam('name');
		$age = $request -> getParam('age');
		$phone = $request -> getParam('phone');
		$roleid = $request -> getParam('roleid');
		$email = $request -> getParam('email');
		$address = $request -> getParam('address');
		$id = $request -> getParam('id');
		$time=date('Y-m-d H:i:s',time());
		$user = new UserModel();
		$data = array('UserName' =>$username,'Phone'=>$phone, 'age' => $age, 'RoleID' => $roleid,'Email' => $email,'address'=>$address, 'Password' => "123123",'Time'=>$time);
		$ret=$user -> update($id,$data);
echo json_encode([
  'code'=>$ret
]);
		
		
		
	}

	public function UserItem($request, $response, $args) {
		$user = new UserModel();
		$user -> UserItem();
	}
}
?>
